package com.wanxi.login.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:Iny
 * @Data：2021/10/27 11:24
 */
public class UploadUtil {

    public static Map uploadFile(MultipartFile file, HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();
        String filename = System.currentTimeMillis() / 1000 + file.getOriginalFilename();
        try {
//            file.transferTo(new File(outs[0] + "src\\main\\webapp\\img\\" + filename));
            file.transferTo(new File("H:/培训/JAVA/upload/" + filename));
//            file.transferTo(new File("H:\\培训\\JAVA\\front\\img\\" + filename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        String scheme = req.getScheme();
        String servletName = req.getServerName();
        int localPort = req.getLocalPort();
        String path = scheme + "://" + servletName + ":" + localPort + "/img/" + filename;
        map.put("src", path);
        map.put("title", filename);
        return map;
    }
}
